import requests
import aiohttp
import  asyncio
import threading
import time

t = int(time.time() * 1000)
cookies = {
    'fullscreengg': '1',
    'fullscreengg2': '1',
    'qgqp_b_id': '1f427175c13479ad95c5ed70874e0591',
    'st_si': '25283373745137',
    'st_pvi': '72050002787228',
    'st_sp': '2025-11-04%2014%3A04%3A31',
    'st_inirUrl': '',
    'st_sn': '1',
    'st_psi': '20251104140431631-113200301321-4216076820',
    'st_asi': 'delete',
    'st_nvi': 'gH2bU4vqPGP_5KDOpdYhl3672',
    'nid': '010d039dd427dc4d187090491f47d7ad',
    'nid_create_time': '1762236272280',
    'gvi': 'ikTUd-0c1WNW3Qf3ge07be982',
    'gvi_create_time': '1762236272280',
}

headers = {
    'Accept': '*/*',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'Connection': 'keep-alive',
    'Referer': 'https://quote.eastmoney.com/center/gridlist.html',
    'Sec-Fetch-Dest': 'script',
    'Sec-Fetch-Mode': 'no-cors',
    'Sec-Fetch-Site': 'same-site',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36',
    'sec-ch-ua': '"Chromium";v="142", "Google Chrome";v="142", "Not_A Brand";v="99"',
    'sec-ch-ua-mobile': '?0',
    'sec-ch-ua-platform': '"Windows"',
    # 'Cookie': 'fullscreengg=1; fullscreengg2=1; qgqp_b_id=1f427175c13479ad95c5ed70874e0591; st_si=25283373745137; st_pvi=72050002787228; st_sp=2025-11-04%2014%3A04%3A31; st_inirUrl=; st_sn=1; st_psi=20251104140431631-113200301321-4216076820; st_asi=delete; st_nvi=gH2bU4vqPGP_5KDOpdYhl3672; nid=010d039dd427dc4d187090491f47d7ad; nid_create_time=1762236272280; gvi=ikTUd-0c1WNW3Qf3ge07be982; gvi_create_time=1762236272280',
}


# #
# for page in range(1,273):
#     url = f'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A81%2Bs%3A262144%2Bf%3A!2&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn={page}&pz=20&po=1&dect=1&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_={t}'
#     res = requests.get(url,headers=headers, cookies=cookies)
#     print(res.json())


#异步技术
# async def fetch(session,url):
#     async with session.get(url) as response:
#         return await response.json()
#
# async def main():
#     async with aiohttp.ClientSession(headers=headers,cookies=cookies) as session:
#         urls = [f'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A81%2Bs%3A262144%2Bf%3A!2&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn={page}&pz=20&po=1&dect=1&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_={t}' for page in range(1,274)]
#         # f = [fetch(session,url) for url in urls]
#         p = 3
#         for i in range(0,len(urls),p):
#             p_urls = urls[i:i+p]
#             p_f = [fetch(session,url) for url in p_urls]
#             values = await asyncio.gather(*p_f)
#             # print(values)
#             for v in values:
#                 print(v['data'])
#             await asyncio.sleep(1)
# asyncio.run(main())



# 多线程
# 手动开启多个线程  线程结束后就关闭了 开销太大  推荐使用线程池
# def t_main(url):
#     res = requests.get(url, headers=headers,cookies=cookies)
#     print(threading.current_thread().name,res.json())
#
#
# def main(t):
#     urls = [f'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A81%2Bs%3A262144%2Bf%3A!2&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn={page}&pz=20&po=1&dect=1&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_={t}' for page in range(1, 4)]
#     l = []
#     for url in urls:
#         t= threading.Thread(target=t_main, args=(url,))
#         t.start()
#         l.append(t)
#     for i in l:
#         i.join()
#
# if __name__ == '__main__':
#
#     main(t)


# 线程池
urls = [f'https://push2.eastmoney.com/api/qt/clist/get?np=1&fltt=1&invt=2&fs=m%3A0%2Bt%3A6%2Bf%3A!2%2Cm%3A0%2Bt%3A80%2Bf%3A!2%2Cm%3A1%2Bt%3A2%2Bf%3A!2%2Cm%3A1%2Bt%3A23%2Bf%3A!2%2Cm%3A0%2Bt%3A81%2Bs%3A262144%2Bf%3A!2&fields=f12%2Cf13%2Cf14%2Cf1%2Cf2%2Cf4%2Cf3%2Cf152%2Cf5%2Cf6%2Cf7%2Cf15%2Cf18%2Cf16%2Cf17%2Cf10%2Cf8%2Cf9%2Cf23&fid=f3&pn={page}&pz=20&po=1&dect=1&ut=fa5fd1943c7b386f172d6893dbfba10b&wbp2u=%7C0%7C0%7C0%7Cweb&_={t}' for page in range(1, 4)]
import concurrent.futures
import os
# c = os.cpu_count()*0.5

def fetch(url):
    res = requests.get(url,headers=headers,cookies=cookies)
    print(res.json())

    # res = requests.get(url,headers=headers,cookies=cookies)
    # return res.json()

with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    for url in urls:
        executor.submit(fetch, url)

    # futures = [executor.submit(fetch,url) for url in urls]
    # for futures in concurrent.futures.as_completed(futures):
    #     print(futures.result())




